﻿using Device.API.Applcation.Command.EquipmentCommand;
using Device.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Device.API.Controllers
{
    /// <summary>
    /// 设备控制器(写)
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class EquipmentController : ControllerBase
    {


        private readonly IMediator _mediator;
        public EquipmentController(IMediator mediator)
        {
            _mediator = mediator;
        }


        /// <summary>
        /// 添加设备
        /// </summary>
        /// <param name="command"> </param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddEquipment(CreateEquipmentCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }

        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> DelEquipment(BatchDelEquipmentCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 更新设备信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UptEquipment(UptEquipmentCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
                if (result.Data == -1)
                {
                    result.Message = "名称已存在";
                }
                if (result.Data == -2)
                {
                    result.Message = "编码重复";
                }
            }
            catch (Exception ex)
            {
                result.Code = -100;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }







    }
}
